import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 新增满减活动
///
class ReduceAdd extends StatefulWidget {
  const ReduceAdd({Key? key}) : super(key: key);

  @override
  State<ReduceAdd> createState() => _ReduceAddState();
}

class _ReduceAddState extends State<ReduceAdd> {
  bool isNew = true;

  double get leftWidth {
    return Sizes.default90;
  }

  double get rowHeight {
    return Sizes.default60;
  }

  TextStyle get style {
    return StylesUtils.customTextStyle(color: ColorsUtils.color333);
  }

  List<int> len = [];

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 5; i++) {
      len.add(i);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('创建满减活动'),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: leftWidth,
                        alignment: Alignment.topLeft,
                        padding: const EdgeInsets.only(top: Sizes.default10),
                        child: Text(
                          '满减规则',
                          style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                        ),
                      ),
                      Expanded(
                          child: Column(
                              children: len
                                  .asMap()
                                  .map((key, value) => MapEntry(
                                      key,
                                      Container(
                                        margin: EdgeInsets.only(
                                            bottom: key == len.length - 1 ? 0 : Sizes.default15),
                                        child: Row(
                                          children: [
                                            Text(
                                              '满',
                                              style: style,
                                            ),
                                            Container(
                                              constraints: const BoxConstraints.tightFor(
                                                  width: Sizes.default90, height: Sizes.default40),
                                              margin: const EdgeInsets.symmetric(
                                                  horizontal: Sizes.default15),
                                              decoration: BoxDecoration(
                                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                                  border: Border.all(
                                                      color: ColorsUtils.rgba(228, 228, 228, 1))),
                                              child: TextField(
                                                style: StylesUtils.customTextStyle(
                                                    color: ColorsUtils.color363,
                                                    fontSize: Sizes.default15),
                                                decoration: InputDecoration(
                                                    border: const OutlineInputBorder(
                                                      borderSide: BorderSide.none,
                                                    ),
                                                    isCollapsed: true,
                                                    filled: true,
                                                    fillColor: Colors.transparent,
                                                    contentPadding: const EdgeInsets.symmetric(
                                                        horizontal: Sizes.default10,
                                                        vertical: Sizes.default10),
                                                    constraints: const BoxConstraints(
                                                        minHeight: Sizes.default40),
                                                    hintText: '',
                                                    hintStyle: StylesUtils.customTextStyle(
                                                        fontSize: Sizes.default14,
                                                        color: ColorsUtils.color999)),
                                                keyboardType: TextInputType.number,
                                                textAlign: TextAlign.center,
                                                onChanged: (String content) {},
                                              ),
                                            ),
                                            Text('减', style: style),
                                            Container(
                                              constraints: const BoxConstraints.tightFor(
                                                  width: Sizes.default90, height: Sizes.default40),
                                              margin: const EdgeInsets.only(left: Sizes.default15),
                                              decoration: BoxDecoration(
                                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                                  border: Border.all(
                                                      color: ColorsUtils.rgba(228, 228, 228, 1))),
                                              child: TextField(
                                                style: StylesUtils.customTextStyle(
                                                    color: ColorsUtils.color363,
                                                    fontSize: Sizes.default15),
                                                decoration: InputDecoration(
                                                    border: const OutlineInputBorder(
                                                      borderSide: BorderSide.none,
                                                    ),
                                                    isCollapsed: true,
                                                    filled: true,
                                                    fillColor: Colors.transparent,
                                                    contentPadding: const EdgeInsets.symmetric(
                                                        horizontal: Sizes.default10,
                                                        vertical: Sizes.default10),
                                                    constraints: const BoxConstraints(
                                                        minHeight: Sizes.default40),
                                                    hintText: '',
                                                    hintStyle: StylesUtils.customTextStyle(
                                                        fontSize: Sizes.default14,
                                                        color: ColorsUtils.color999)),
                                                keyboardType: TextInputType.number,
                                                textAlign: TextAlign.center,
                                                onChanged: (String content) {},
                                              ),
                                            ),
                                          ],
                                        ),
                                      )))
                                  .values
                                  .toList())),
                    ],
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                          width: leftWidth,
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '用户类型',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Wrap(
                            spacing: Sizes.default10,
                            alignment: WrapAlignment.start,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              InkWell(
                                onTap: () {
                                  setState(() {
                                    isNew = true;
                                  });
                                },
                                child: Container(
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints.expand(
                                      width: 110, height: Sizes.default30),
                                  decoration: changeBoxDecoration(isNew),
                                  child: Text(
                                    '店铺新用户',
                                    style: isNew
                                        ? StylesUtils.customTextStyle(color: ColorsUtils.colorFf6)
                                        : StylesUtils.customTextStyle(color: ColorsUtils.color333),
                                  ),
                                ),
                              ),
                              InkWell(
                                onTap: () {
                                  setState(() {
                                    isNew = false;
                                  });
                                },
                                child: Container(
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints.expand(
                                      width: 110, height: Sizes.default30),
                                  decoration: changeBoxDecoration(!isNew),
                                  child: Text('店铺所有用户',
                                      style: !isNew
                                          ? StylesUtils.customTextStyle(color: ColorsUtils.colorFf6)
                                          : StylesUtils.customTextStyle(
                                              color: ColorsUtils.color333)),
                                ),
                              ),
                            ],
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '活动日期',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: InkWell(
                            child: Container(
                              height: Sizes.default45,
                              padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('请选择起止日期',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default15, color: ColorsUtils.color797)),
                                  BrunoTools.getAssetSizeImage(BrnAsset.iconRightArrow, 16, 16)
                                ],
                              ),
                            ),
                            onTap: () {
                              ToolsUtils.openDateRangePicker(context, onConfirm:
                                  (DateTime startDateTime, DateTime endDateTime,
                                      List<int> startSelectedIndex, List<int> endSelectedIndex) {
                                setState(() {});
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '使用方式',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '移动端',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              alignment: Alignment.centerLeft,
              child: Text(
                '温馨提示：活动审核通过后无法修改',
                style: StylesUtils.customTextStyle(
                    fontSize: Sizes.default13, color: ColorsUtils.color999),
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        child: Container(
          height: Sizes.default60,
          alignment: Alignment.center,
          child: FractionallySizedBox(
            widthFactor: 0.8,
            heightFactor: 0.8,
            child: GFButton(
                onPressed: () {},
                text: '创建',
                textStyle: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                shape: GFButtonShape.pills,
                type: GFButtonType.solid,
                size: GFSize.LARGE,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6)),
          ),
        ),
      ),
    );
  }

  BoxDecoration changeBoxDecoration([bool isActive = true]) {
    if (isActive) {
      return BoxDecoration(
          color: ColorsUtils.rgba(255, 238, 238, 1),
          borderRadius: BorderRadius.circular(Sizes.default15),
          border: Border.all(color: ColorsUtils.rgba(255, 110, 83, 1)));
    }
    return BoxDecoration(
      color: ColorsUtils.rgba(245, 245, 245, 1),
      borderRadius: BorderRadius.circular(Sizes.default15),
    );
  }
}
